import os
import sys

import django
from lxml import etree
from importlib import import_module


def first(tree, xpath):
    es = tree.xpath(xpath)
    assert es, "not found"
    return es[0]


def convert_num(text):
    c = text[-1]
    if c in 'kK':
        num = text[:-1]
        res = int(num) * 1024
    elif c in 'mM':
        num = text[:-1]
        res = int(num) * 1024 * 1024
    else:
        res = int(text)
    return res


def parse(text):
    tree = etree.HTML(text)

    xpath = '//div[@id="question-mini-list"]/div/div'
    divs = tree.xpath(xpath)

    for div in divs:
        xpath = './/div/span/text()'
        votes, answer, views = div.xpath(xpath)[:3]
        votes = convert_num(votes)
        answer = convert_num(answer)
        views = convert_num(views)
        xpath = './/div[@class="summary"]//a'
        a = first(div, xpath)
        subject = a.text
        url = a.get('href')
        record = {'votes': votes, 'answer': answer, 'views': views,
                  'subject': subject, 'url': url}
        q = models.Question(subject=subject, url=url,
                            votes=votes, answer=answer, views=views)
        q.save()


def load_models(path, project, app):
    sys.path.insert(0, path)
    mod_name = '%s.settings' % project
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", mod_name)
    django.setup()
    models = import_module('%s.models' % app)
    sys.path.remove(path)
    return models


path = os.path.join(os.path.dirname(__file__), 'db')
project = 'db'
app = 'store'
models = load_models(path, project, app)


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('usage: %s file' % os.path.basename(sys.argv[0]))
        exit(1)

    file_path = sys.argv[1]
    text = open(file_path).read()
    parse(text)
